import { APP_NAME, DEVICE_ID, USER_ID } from '../constants';
import { get, post } from '../utils/http';
import { getStorageItem } from '@/utils/storage';

export const memberAPI = {
  getProductList: async () => {
    try {
      const { data } = await get<Member.GetProductRes>('/payment-service/payment/products/list');
      return data.data;
    } catch (error) {
      console.log(error);
      throw new Error('查询产品列表失败');
    }
  },
  // 查询用户会员订阅信息
  getUserMemberInfo: async () => {
    try {
      const res = await get<Member.GetMemberInfoRes>(
        '/member-service/member/subscriptions/member-info',
        {
          equipmentNum: getStorageItem(DEVICE_ID),
        },
        {
          'ts-user-id': getStorageItem(USER_ID),
        },
      );
      return res.data;
    } catch (error) {
      console.log(error);
      throw new Error('查询用户会员订阅信息失败');
    }
  },
  //  查询用户试用次数
  getUserTrial: async (): Promise<Member.UserTrails> => {
    try {
      const res = await get<Member.UserTrails>('/member-service/member/user-trails', undefined, {
        'ts-app-name': APP_NAME,
      });
      return res.data;
    } catch (error) {
      console.log(error);
      throw new Error('查询用户试用次数失败');
    }
  },
  //   新增用户试用日志，后台减少一次试用次数
  logUserTrial: async () => {
    try {
      await post('/member-service/member/user-trails', undefined, {
        headers: {
          'ts-app-name': APP_NAME,
        },
      });
    } catch (error) {
      console.log(error);
      throw new Error('新增用户试用日志失败');
    }
  },
  // 获取用户订阅是否过期
  getSubscriptionExpire: async () => {
    try {
      const res = await get<Member.SubscriptionExpire>(
        '/member-service/member/subscriptions/expired',
        {},
        {
          'ts-app-name': APP_NAME,
        },
      );
      return res.data;
    } catch (error) {
      console.log(error);
      throw new Error('查询用户订阅是否过期失败');
    }
  },
};
